package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig5 {
//    正常的交换机和队列
    public static  final String EXCHANGE_NAME = "order_exchange";
    public static  final String QUEUE_NAME = "order_queue";
//    1. 交换机
    @Bean("order_exchange")
    public Exchange bootExchange1(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
//    2. Queue队列

//    设置死信交换机的名称
//    设置
    Map<String,Object> args = new HashMap<>();

    @Bean("order_queue")
    public Queue bootQueue1(){

        return QueueBuilder.durable(QUEUE_NAME)
                .withArgument("x-dead-letter-exchange","order_exchange_dlx")
                .withArgument("x-dead-letter-routing-key","dlx.haha")
                .withArgument("x-message-ttl",10000L).build();
    }
//    3. 队列和交换机的绑定关系 Binding
    @Bean
    public Binding bindQueueExchange(@Qualifier("order_queue") Queue queue, @Qualifier("order_exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("order.#").noargs();
    }



    public static  final String DEAD_EXCHANGE_NAME = "order_exchange_dlx";
    public static  final String DEAD_QUEUE_NAME = "order_queue_dlx";
    //    1. 交换机
    @Bean("order_exchange_dlx")
    public Exchange bootExchange2(){
        return ExchangeBuilder.topicExchange(DEAD_EXCHANGE_NAME).durable(true).build();
    }
    //    2. Queue队列
    @Bean("order_queue_dlx")
    public Queue bootQueue2(){
        return QueueBuilder.durable(DEAD_QUEUE_NAME).build();
    }
    //    3. 队列和交换机的绑定关系 Binding
    @Bean
    public Binding bindQueueExchange2(@Qualifier("order_queue_dlx") Queue queue, @Qualifier("order_exchange_dlx") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }


}
